import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement; 

public class login {

	/**
	 * Author Rickard Bremer 810205-4890 
	 * Login function , recives user and pass from User and opens GUI_Database if login is successfull.
	 */
	public static String url = "jdbc:mysql://localhost:3306/";
	public static String dbName = "lindholmen";
	public static String driver = "com.mysql.jdbc.Driver";
	public static String dbuser ="root";
	public static String dbpass ="";
	
	public static void userpass(String username , String password) {
		
		
			
		
		String sql = "SELECT * FROM user_account  WHERE UName='" + username + "' AND Password='" + password + "'";
		
		System.out.println("Security checking.");

		Connection con = null;

		
		
		try {

			Class.forName(driver).newInstance();
			con = DriverManager.getConnection(url + dbName, dbuser, dbpass);
			System.out.println("Connected to the database");
			System.out.println("-------------------------");
			
			System.out.println("Checking Username and Password");
			Statement stmt = (Statement) con.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            
            while (rs.next()) {
            String checkUser =  rs.getString("UName");
            String checkPass = rs.getString("Password");
            
            
            
            if (username.equals(checkUser) && password.equals(checkPass)){
            	System.out.println("Hello!!! you logged in");
            	GUI_Database.edit();
            }
            
            else {
            	System.out.println("Login Failed...");
            	System.out.println("Username or/and Password is incorrect");
            }
            	
            }
            
			con.close();
		    System.out.println("Disconnected from database");
		
		} catch (Exception e) {
			System.out.println("Login crashed!");
			e.printStackTrace();
		}
		
	
	
	    }
     }


